---
title: Batching operations
---

Apollo Kotlin supports batching multiple operations in a single HTTP request to reduce the number of network round trips. Whenever you execute an operation with batching enabled, your client waits a short time interval to collect any _other_ batchable operations that are also executed. When the interval completes, your client sends all the operations in a single HTTP request.

Note that due to the waiting interval, batching adds some latency to operations. Operations that require as little latency as possible can [opt out of batching](#opting-outin).

## Enabling batching

You enable batching when you initialize your `ApolloClient` instance, like so:

```kotlin {3}
val apolloClient = ApolloClient.Builder()
  .serverUrl("https://example.com/graphql")
  .httpBatching()
  .build()
```

By default, the batching engine waits 10ms to collect queries before sending them, and a single batch contains a maximum
of 10 queries. You can configure these defaults by passing additional configuration options to the `httpBatching`
method:

```kotlin
apolloClientBuilder.httpBatching(
    batchIntervalMillis = 50, // Wait 50ms
    maxBatchSize = 20         // Max 20 queries
)
```

## Opting out/in

By default, if you [enable batching](#enabling-batching), it's enabled for _all_ operations. An individual operation can
opt _out_ of batching by passing `false` to the `canBeBatched` method:

```kotlin
apolloClient.query(MyQuery()).canBeBatched(false).execute()
```

This is helpful if there's a particular operation that you always want to execute with as little latency as possible.

You can also require individual operations to _opt in_ to batching. To do so, you first call `canBeBatched(false)` while initializing your `ApolloClient` instance:

```kotlin {4}
val apolloClient = ApolloClient.Builder()
  .serverUrl("https://example.com/graphql")
  .httpBatching()
  .canBeBatched(false)
  .build()
```

Now, batching is _disabled_ by default, and individual operations can opt _in_ with `canBeBatched(true)`.
